package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.enums.LindiContractTemplateFieldEnum;
import com.minimalist.common.entity.lindi.po.LindiContractTemplateField;
import com.minimalist.common.entity.lindi.po.LindiContractTemplateFieldOption;
import com.minimalist.common.entity.lindi.vo.contracttemplatefield.LindiContractTemplateFieldQueryVO;
import com.minimalist.common.entity.lindi.vo.contracttemplatefield.LindiContractTemplateFieldVO;
import com.minimalist.common.entity.lindi.vo.contracttemplatefieldoption.LindiContractTemplateFieldOptionVO;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiContractTemplateFieldMapper;
import com.minimalist.lindi.mapper.LindiContractTemplateFieldOptionMapper;
import com.minimalist.lindi.service.LindiContractTemplateFieldService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标平台_合同模板字段表 服务层实现。
 *
 * @author 11987
 * @since 2025-05-31
 */
@Service
public class LindiContractTemplateFieldServiceImpl extends
        ServiceImpl<LindiContractTemplateFieldMapper, LindiContractTemplateField> implements LindiContractTemplateFieldService{

    @Autowired
    private LindiContractTemplateFieldMapper mapper;

    @Autowired
    private LindiContractTemplateFieldOptionMapper contractTemplateFieldOptionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addContractTemplateField(
            LindiContractTemplateFieldVO contractTemplateFieldVO){
        LindiContractTemplateField field = BeanUtil.copyProperties(
                contractTemplateFieldVO, LindiContractTemplateField.class);
        field.setContractTemplateFieldId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(field, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                LindiContractTemplateFieldEnum.ErrorMsgEnum
                        .ADD_CONTRACT_TEMPLATE_FIELD_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContractTemplateFieldByContractTemplateFieldId(
            Long contractTemplateFieldId){
        LindiContractTemplateField field = mapper
                .selectContractTemplateFieldByContractTemplateFieldId(
                        contractTemplateFieldId);
        Assert.notNull(field, () -> new BusinessException(
                LindiContractTemplateFieldEnum.ErrorMsgEnum
                        .CONTRACT_TEMPLATE_FIELD_NOT_FOUND.getDesc()));
        int delete = mapper.delete(field);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                LindiContractTemplateFieldEnum.ErrorMsgEnum
                        .DELETE_CONTRACT_TEMPLATE_FIELD_ERROR.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContractTemplateFieldByContractTemplateFieldId(
            LindiContractTemplateFieldVO contractTemplateFieldVO){
        LindiContractTemplateField field = mapper
                .selectContractTemplateFieldByContractTemplateFieldId(
                        contractTemplateFieldVO.getContractTemplateFieldId());
        Assert.notNull(field, () -> new BusinessException(
                LindiContractTemplateFieldEnum.ErrorMsgEnum
                        .CONTRACT_TEMPLATE_FIELD_NOT_FOUND.getDesc()));
        BeanUtil.copyProperties(contractTemplateFieldVO, field
                , CopyOptions.create().ignoreNullValue());
        int update = mapper.update(field);
        Assert.isTrue(update == 1, () -> new BusinessException(
                LindiContractTemplateFieldEnum.ErrorMsgEnum
                        .UPDATE_CONTRACT_TEMPLATE_FIELD_ERROR.getDesc()));
    }

    @Override
    public PageResp<LindiContractTemplateFieldVO> getPageContractTemplateFieldList(
            LindiContractTemplateFieldQueryVO queryVO){
        // 分页查询
        Page<LindiContractTemplateField> fieldPage = mapper
                .selectPageContractTemplateFieldList(queryVO);
        // 数据转换
        List<LindiContractTemplateFieldVO> voList = BeanUtil.copyToList(
                fieldPage.getRecords(), LindiContractTemplateFieldVO.class);
        return new PageResp<>(voList, fieldPage.getTotalRow());
    }

    @Override
    public LindiContractTemplateFieldVO
    getContractTemplateFieldByContractTemplateFieldId(Long contractTemplateFieldId){
        LindiContractTemplateField field = mapper
                .selectContractTemplateFieldByContractTemplateFieldId(contractTemplateFieldId);
        Assert.notNull(field, () -> new BusinessException(
                LindiContractTemplateFieldEnum.ErrorMsgEnum
                        .CONTRACT_TEMPLATE_FIELD_NOT_FOUND.getDesc()));

        LindiContractTemplateFieldVO fieldVO = BeanUtil.copyProperties(
                field, LindiContractTemplateFieldVO.class);

        // 查询关联的选项列表
        List<LindiContractTemplateFieldOption> optionList =
                contractTemplateFieldOptionMapper
                        .selectContractTemplateFieldOptionListByContractTemplateFieldId(
                                contractTemplateFieldId);
        List<LindiContractTemplateFieldOptionVO> optionVOList = BeanUtil.copyToList(
                optionList, LindiContractTemplateFieldOptionVO.class);
        fieldVO.setOptions(optionVOList);

        return fieldVO;
    }
}
